package com.ethen.liveeventbus.slice;

import android.component.CompatSlice;
import android.log.Log;
import com.ethen.liveeventbus.ResourceTable;
import com.ohos.liveeventbus.LiveEventBus;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;

public class LifeAbilitySlice2 extends CompatSlice {
    private static final String TAG = "LiveEventAbilitySlice2";
    private static final String KEY1 = "event_live";
    private static final String KEY2 = "event_live2";

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_life2_request_layout);

        findComponentById(ResourceTable.Id_btn_life).setClickedListener(component -> {
            LiveEventBus.get(KEY1).post("hello ethen!4");
        });

        findComponentById(ResourceTable.Id_btn_life_global).setClickedListener(component -> {
            LiveEventBus.get(KEY2).post("hi ethen444444!");
        });

        initSliceViewModel();
        initGlobalViewModel();
    }

    private void initSliceViewModel() {
        LiveEventBus.get(KEY1, String.class).observe(this, s -> {
            new ToastDialog(getContext()).setText(s).setDuration(1000).show();
            Log.d(TAG, "get String.class,key: " + KEY1 + ",value is:" + s);
        });

        LiveEventBus.get(KEY1, String.class).observeSticky(this, s -> {
            new ToastDialog(getContext()).setText(s).setDuration(1000).show();
            Log.d(TAG, "get Sticky String.class,key: " + KEY1 + ",value is:" + s);
        });
    }

    private void initGlobalViewModel() {

    }
}
